/**
* \file final.c
* \brief Programme de tests.
* \author walid
* \version 0.1
* \date 24 mai 2013
*
* Programme de test 
*/
#include <allegro.h>
/* une structure qui contient 4 pointeurs de type BITMAP
(3images de déplacement a gauche et 3 de déplacement droite , une en haut et une en bas)
2 variable de types float c'est la position selon x et y de notre crocodile */
typedef struct croc
{
float x ; float y;
BITMAP *right[3];
BITMAP *left[3];
BITMAP *up;
BITMAP *down;
}croc;
int main() {
/* b,c,d,e se sont des variables qu'on teste avec
 la collision entre les clefs et le crocodile*/
int b=0;
int c=0;
int d=0;
int e=0;
/*c'est la position stable de notre score*/
int x_score=1;
int y_score=1;


/*une fonction getpixel qui prend x et y 
et un pointeur sur le buffer comme paramétre */
int get_pixel(BITMAP* Buffer,int x,int y ) ; 

// declaration des differents variables de types bitmap
BITMAP *obstacle;
BITMAP *obstacle2;
BITMAP *obstacle3;

BITMAP *game_over;


BITMAP *bonus;
BITMAP *bonus1;
BITMAP *bonus2;
BITMAP *bonus3;


BITMAP * ima;
BITMAP *bonus_col;
BITMAP *Buffer;
BITMAP *Image;
BITMAP *back_col;
BITMAP *Background;
BITMAP *image[3];
BITMAP *score[5];
//declaration de deux poiteurs de type sample pour le sons
SAMPLE *Sound;
SAMPLE *sound;
/*declaration et affectation de differents position initial selon l'axe y pour
le déplacement de l'bstacle*/ 

int yin=395;
int yin2=0;
int yin3=320; // l'axe x deplacement initial

/*declaration et affectation de differents position qu'il doit parcourir
selon l'axe y avec une initialisation*/
int y1=395;
int y2=30;
int y3=320; // l'axe x deplacement selon x

/*declaration et affectation de differents position initial selon l'axe y pour
le déplacement de l'bstacle*/ 

int yfin=790;
int yfin2=330;
int yfin3=690;

/*declaration et affectation de differents position final selon l'axe x pour l'bstacle*/ 
int x_in2=450;
int x_in=1041;
int x_in3=460;//

//deplacement aleatoire variable de teste
int a=2;
int f=2;
int g=0;

//declaration et affectation des position de  chaque clef
int x_clef=10;
int y_clef=300;
int x_clef1=750;
int y_clef1=135;
int x_clef2=550;
int y_clef2=330;
int x_clef3=960;
int y_clef3=550;

int i=0;//pour parcourir les images de crocodile
int j=0;//pour parcourir les images de menu
int u=0;//pour parcourir les images des numéros de score de 0 jusqu'a 4

croc C;

/* General initialization function */
allegro_init();
if (install_keyboard() == -1) {
allegro_message("Error! %s", allegro_error);
return -1;
}
set_color_depth(32);
if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 1500, 1000, 0, 0) != 0) {
set_gfx_mode(GFX_TEXT, 0, 0, 0, 0); /* heres the command for text mode */
allegro_message("Unable to initialize video mode!\n%s\n", allegro_error);
return 1;
}
//**Sound**/

if(install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL)) {
allegro_message("Sound Error: %s", allegro_error);
return 1; }

/*Volume*/

set_volume(255, 255); //digital and music to max levels

/*Load*/

sound = load_sample("amb3.wav");
play_sample(sound, 255, 128, 1000, 0);
if (!sound)
{allegro_message("Error reading wav file");
return 1;}


Buffer = create_bitmap(1200, 800);

image[0]=load_bitmap("menu.bmp",NULL);
image[1]=load_bitmap("menu.bmp",NULL);
image[2]=load_bitmap("menu.bmp",NULL);
image[3]=load_bitmap("menu.bmp",NULL);
//loading image menu

bonus=load_bitmap("clef.bmp",NULL);
bonus1=load_bitmap("clef.bmp",NULL);
bonus2=load_bitmap("clef.bmp",NULL);
bonus3=load_bitmap("clef.bmp",NULL);
//loading image bonus

game_over=load_bitmap("game.bmp",NULL);
//loading image game over

score[0]=load_bitmap("00.bmp", NULL);
score[1]=load_bitmap("01.bmp",NULL);
score[2]=load_bitmap("02.bmp", NULL);
score[3]=load_bitmap("03.bmp",NULL);
score[4]=load_bitmap("04.bmp",NULL);
//loading image de score

ima=score[u];




obstacle=load_bitmap("obs.bmp",NULL);
obstacle2=load_bitmap("obs.bmp",NULL);
obstacle3=load_bitmap("obs.bmp",NULL);
//loading obstacle

bonus_col=load_bitmap("cle1.bmp",NULL);
//loading background collision

back_col=load_bitmap("collision.bmp" , NULL);
//loading background collision

Background = load_bitmap("background.bmp", NULL);
//loading background 

C.right[0] = load_bitmap("1.bmp", NULL);
C.right[1] = load_bitmap("2.bmp", NULL);
C.right[2] = load_bitmap("3.bmp", NULL);
C.left[0] = load_bitmap("4.bmp", NULL);
C.left[1] = load_bitmap("5.bmp", NULL);
C.left[2] = load_bitmap("6.bmp", NULL);
C.up=load_bitmap("1.bmp", NULL);
C.down=load_bitmap("1.bmp", NULL);
//loading images deplacement crocodile
C.x=0; C.y=150;

if ((Background == NULL)||(C.left[0]==NULL))
{
allegro_message("could not load images");
allegro_exit();
}
i=0;
blit(Background, Buffer, 0, 0, 0, 0, Background->w, Background->h);
Image = load_bitmap("1.bmp", NULL);
draw_sprite(Buffer,Image, C.x, C.y);


while(!key[KEY_ESC])
{



	if(key[KEY_DOWN])
		j++;
	else
	if(key[KEY_UP])
		j--;
	if(j==4)
		j=0;
	if(key[KEY_ENTER])
	{
		if(j==1)
		{
stop_sample(sound);

			clear_bitmap(Buffer);

			blit(Background, Buffer, 0, 0, 0, 0, Background->w, Background->h);
Image = load_bitmap("1.bmp", NULL);

draw_sprite(Buffer,Image, C.x, C.y);


while (!key[KEY_ESC])
{
		set_volume(255, 255); //digital and music to max levels

/*Load*/

Sound = load_sample("bubble.wav");
play_sample(Sound, 255, 128, 1000, 0);
if (key[KEY_RIGHT])

{
	

if (C.x<1450)
{
clear_bitmap(Buffer);
i++;
C.x=C.x+1;
if( ! (get_pixel(back_col, C.x+45,C.y )== 255))
{
C.x=C.x-10;
}

if (i>2)
{
i=0;
}
blit(Background, Buffer, 0, 0, 0, 0, Background->w, Background->h);
Image = C.right[i];
draw_sprite(Buffer,Image, C.x, C.y);
}
}
if (key[KEY_LEFT] )
{
if (C.x>0)
{
clear_bitmap(Buffer);
i++;
C.x=C.x-1;
if( ! (get_pixel(back_col, C.x,C.y )== 255))
{
C.x=C.x+10;
}

if (i>2)
{
i=0;
}
blit(Background, Buffer, 0, 0, 0, 0, Background->w, Background->h);
Image = C.left[i];
draw_sprite(Buffer,Image, C.x, C.y);
}
}
if (key[KEY_DOWN])
{
if (C.y<750)
{
clear_bitmap(Buffer);
C.y=C.y+1;
if( ! (get_pixel(back_col, C.x,C.y+40 )== 255))
{
C.y=C.y-10;
}

blit(Background, Buffer, 0, 0, 0, 0, Background->w, Background->h);
Image = C.down;
draw_sprite(Buffer,Image, C.x, C.y);
}
}
if (key[KEY_UP] )
{
if (C.y>0)
{
clear_bitmap(Buffer);
C.y=C.y-1;
if( ! (get_pixel(back_col, C.x,C.y )== 255))
{
C.y=C.y+10;
}

blit(Background, Buffer, 0, 0, 0, 0, Background->w, Background->h);
Image = C.up;
draw_sprite(Buffer,Image, C.x, C.y);
}
if((x_clef-40<C.x)&&(C.x<x_clef+40)&&(y_clef-1<C.y)&& (C.y<y_clef+1))
	{
		b=1;
	}
	
}

if(b==0)
	{
		
		draw_sprite(Buffer,bonus, x_clef, y_clef);

		


	} else
	if(b==1)
	{
		u=1;
				draw_sprite(Buffer,ima, x_score, y_score);

	}
			

	if((x_clef1-40<C.x)&&(C.x<x_clef1+40)&&(y_clef1-1<C.y)&& (C.y<y_clef1+1))
	{
		c=1;
	}
	if(c==0)
	{

		draw_sprite(Buffer,bonus1, x_clef1, y_clef1);
		
		


	}
	else if(c==1)
	{
		u=2;
						draw_sprite(Buffer,ima, x_score, y_score);

	}

	if((x_clef2-40<C.x)&&(C.x<x_clef2+40)&&(y_clef2-1<C.y)&& (C.y<y_clef2+1))
	{
		d=1;
	}
	if(d==0)
	{
		draw_sprite(Buffer,bonus2, x_clef2, y_clef2);
		


	}
	else if (d==1)
	{
		u=3;
						draw_sprite(Buffer,ima, x_score, y_score);

			}

	if((x_clef3-40<C.x)&&(C.x<x_clef3+40)&&(y_clef3-1<C.y)&& (C.y<y_clef3+1))
	{
		e=1;
	}
	if(e==0)
	{
		draw_sprite(Buffer,bonus3, x_clef3, y_clef3);
		


	}
	else if(e==1)
	{
		u=4;
					draw_sprite(Buffer,bonus3, x_score, y_score);


	}
	ima=score[u];
draw_sprite(Buffer,ima, x_score,y_score );	
	
	if ((y1<yfin)&&(a==2)) 
{	y1++;

} else a=1;
if ((a==1)&&(y1>yin))
y1--;
else a=2;

{

			draw_sprite(Buffer,obstacle, x_in, y1);
}

			if ((y2<yfin2)&&(f==2)) 
{	y2++;

} else f=1;
if ((f==1)&&(y2>yin2))
y2--;
else f=2;
{

draw_sprite(Buffer,obstacle2, x_in2, y2);
}
			draw_sprite(Buffer,obstacle2, x_in2, y2);
			if ((y3<yfin3)&&(g==2)) 
{	y3++;

} else g=1;
if ((g==1)&&(y3>yin3))
y3--;
else g=2;
			

			if((x_in-40<C.x)&&(C.x<x_in+40)&&(y1-1<C.y)&& (C.y<y1+1))
			{
				
								

blit(Background, Buffer, 0, 0, 0, 0, Background->w, Background->h);

clear_bitmap(Buffer);


blit(game_over, Buffer, 0, 0, 0, 0, game_over->w, game_over->h);
destroy_bitmap(obstacle);
destroy_bitmap(bonus);
destroy_bitmap(bonus);
pause();
            }

            if((x_in2-40<C.x)&&(C.x<x_in2+40)&&(y2-1<C.y)&& (C.y<y2+1))
			{
				
				
						
clear_bitmap(Buffer);

blit(game_over, Buffer, 0, 0, 0, 0, game_over->w, game_over->h);

            }

            if((x_in-40<C.x)&&(C.x<x_in+40)&&(y3-1<C.y)&& (C.y<y3+1))
			{
clear_bitmap(Buffer);

blit(game_over, Buffer, 0, 0, 0, 0, game_over->w, game_over->h);


            }

draw_sprite(Buffer,obstacle, y3, x_in3);

			if(key[KEY_P]) pause();
			if(key[KEY_O]) 
				continue;
			
			
				
			



			

blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
//readkey();
}
destroy_bitmap(Image);
destroy_bitmap(Buffer);
return 0;
}
else if (j==3) return 0;
}

Image=image[j];
blit(Image,Buffer,0,0,0,0,Image->w,Image->h);
blit(Buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
readkey();
}
return 0;
}
END_OF_MAIN();

int get_pixel(BITMAP* Buffer,int x,int y)
{
int tmp;
tmp=getpixel(Buffer,x,y);
return((getr(tmp) + getb(tmp) + getg(tmp)) / 3);
}
